package com.edu.app.controller.linkcontrollers;

import com.edu.app.bean.core.SysActivity;
import com.edu.app.bean.core.SysCustomer;
import com.edu.app.bean.linkbeans.SysCustomerActivity;
import com.edu.app.constant.ErrorConstant;
import com.edu.app.constant.StaticConstant;
import com.edu.app.constant.SysMessage;
import com.edu.app.service.sys.core.SysUserService;
import com.edu.app.service.sys.linkservices.SysCustomerActivityService;
import com.edu.app.util.ErrorUtil;
import com.edu.app.util.JWTUtil;
import com.edu.app.util.MsgUtil;

import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping(value = "/api/customer_activity")
@RestController
public class SysCustomerActivityController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysCustomerActivityService sysCustomerActivityService;

    //使用get需要添加默认get函数
    @GetMapping(value = "")
    @RequiresAuthentication
    public ModelMap getCustomerActivityList(){
        List<SysCustomerActivity> sysCustomerActivityList = sysCustomerActivityService.getObjectList();
        return MsgUtil.retMsg(SysMessage.GET_CUSTOMER_ACTIVITY_SUCCEED, StaticConstant.CUSTOMER_ACTIVITY_LIST, sysCustomerActivityList);
    }

    @GetMapping(value = "/getActivityByCustomerId")
    @RequiresAuthentication
    public ModelMap getActivityByCustomerId(@RequestParam Integer id){
        List<SysActivity> sysActivityList = sysCustomerActivityService.getActivityByCustomerId(id);
        return MsgUtil.retMsg(SysMessage.GET_USER_ACTIVITY_SUCCEED, StaticConstant.ACTIVITY_LIST, sysActivityList);
    }

    @GetMapping(value = "/getCustomerByActivityId")
    @RequiresAuthentication
    public ModelMap getCustomerByActivityId(@RequestParam Integer id){
        List<SysCustomer> sysCustomerList = sysCustomerActivityService.getCustomerByActivityId(id);
        return MsgUtil.retMsg(SysMessage.GET_CUSTOMER_SUCCEED, StaticConstant.CUSTOMER_LIST, sysCustomerList);
    }

    @GetMapping(value = "/getCustomerActivityById")
    @RequiresAuthentication
    public ModelMap getCustomerActivityById(@RequestParam Integer customer_id, @RequestParam Integer activity_id){
        SysCustomerActivity sysCustomerActivity = sysCustomerActivityService.getCustomerActivity(customer_id, activity_id);
        return MsgUtil.retMsg(SysMessage.GET_CUSTOMER_ACTIVITY_SUCCEED, StaticConstant.CUSTOMER_ACTIVITY, sysCustomerActivity);
    }

    /*
     * 为Customer添加角色
     * http://localhost:8080/api/user_roles?user_id=1&role_id=1
     * */
    @PostMapping(value = "")
    @RequiresAuthentication
    public ModelMap addCustomerActivity(@RequestHeader("Authorization") String token,
                                        @RequestParam Integer user_id,
                                        @RequestParam Integer activity_id){

        try{
            SysCustomerActivity sysCustomerActivity = new SysCustomerActivity(user_id, activity_id);
            sysCustomerActivity.initializeSystemColumns(JWTUtil.getUsername(token));
            Integer r = sysCustomerActivityService.add(sysCustomerActivity);
            if(r > 0){
                return MsgUtil.retMsg(SysMessage.ADD_CUSTOMER_ACTIVITY_SUCCEED, null ,null);
            }else{
                return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
            }
        }catch (org.springframework.dao.DuplicateKeyException e){
            return ErrorUtil.retErrMsg(ErrorConstant.CUSTOMER_ACTIVITY_ALREADY_EXISTS);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    /*
     * 删除用户角色
     * http://localhost:8080/api/deleteCustomerRole?user_id=1&role_id=1
     * */
    @DeleteMapping(value = "")
    @RequiresAuthentication
    public ModelMap deleteCustomerActivity(@RequestParam Integer user_id, @RequestParam Integer activity_id){

        SysCustomerActivity sysCustomerActivity = sysCustomerActivityService.getCustomerActivity(user_id, activity_id);

        if(sysCustomerActivity != null){
            try{
                Integer r = sysCustomerActivityService.delete(sysCustomerActivity.getId());
                if(r > 0){
                    return MsgUtil.retMsg(SysMessage.DELETE_CUSTOMER_ACTIVITY_SUCCEED, null ,null);
                }else{
                    return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
                }
            }catch (Exception e){
                return ErrorUtil.retErrMsg(e);
            }
        }else{
            return ErrorUtil.retErrMsg(ErrorConstant.SERVER_INTERNAL_ERROR);
        }
    }

    @GetMapping(value = "/pages/getCustomerActivityByPage/{id}")
    @RequiresAuthentication
    public ModelMap getCustomerActivityByPage(@RequestHeader("Authorization") String token, @PathVariable Integer id, @RequestParam Integer offset, @RequestParam Integer limit){
        try{
            String username = JWTUtil.getUsername(token);
            PageInfo pageInfo = sysCustomerActivityService.getCustomerActivityByPage(id, offset, limit);
            return MsgUtil.retMsg(SysMessage.GET_CUSTOMER_ACTIVITY_SUCCEED, "page_info", pageInfo);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    @GetMapping(value = "/pages/getCustomerActivities/{id}")
    @RequiresAuthentication
    public ModelMap getCustomerActivities(@PathVariable Integer id, @RequestParam String type, @RequestParam Integer offset, @RequestParam Integer limit){
        try{
            PageInfo pageInfo = sysCustomerActivityService.getCustomerActivities(id, type, offset, limit);
            return MsgUtil.retMsg(SysMessage.GET_CUSTOMER_ACTIVITY_SUCCEED, "page_info", pageInfo);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }

    @GetMapping(value = "/pages/getCustomerActivitiesAll/{id}")
    @RequiresAuthentication
    public ModelMap getCustomerActivitiesAll(@PathVariable Integer id, @RequestParam Integer offset, @RequestParam Integer limit){
        try{
            PageInfo pageInfo = sysCustomerActivityService.getCustomerActivitiesAll(id, offset, limit);
            return MsgUtil.retMsg(SysMessage.GET_CUSTOMER_ACTIVITY_SUCCEED, "page_info", pageInfo);
        }catch (Exception e){
            return ErrorUtil.retErrMsg(e);
        }
    }
}
